﻿unit Protocol;

interface

uses
  Winapi.Windows;

const
  (* IOCP相关参数定义       *)
  MAX_BASIC_1K                  =   1024;               (* K计算              *)
  MAX_PACKAGESIZE               =   MAX_BASIC_1K*4;     (* IOCP投递包长度     *)
  RECV_CIRCULAR_BUFFER_SIZE     =   MAX_BASIC_1K*2;     (* 接收环形Buffer长度 *)
  SEND_CIRCULAR_BUFFER_SIZE     =   MAX_BASIC_1K*4;     (* 发送环形Buffer长度 *)
  IO_WORDTHREAD_EXIT            =   $FFFFFFFF;          (* 工作线程退出       *)
  MAX_HEARTBEAT_TIME            =   1000*60*5;          (* 默认心跳时间       *)
  MAX_HEARTBEAT_COUT            =   10;                 (* 心跳次数10次       *)

  (* UDP相关参数定义        *)
  SOCKET_RECEIVE_BUFFER_SIZE    =   256 * MAX_BASIC_1K;	(* 接收缓冲           *)
  SOCKET_SEND_BUFFER_SIZE       =   256 * MAX_BASIC_1K;	(* 发送缓冲           *)


  MAX_ANSWER_NUMBER             =   65535;              (* 最大响应号         *)
  MAX_SEQUENCE_BUFFER_NUMBER    =   65535;              (* IO序列号           *)

type


  (* 引擎启动类型     *)
  TENGINE_START_TYPE = (
    SERVER_ENGINE_START,                                (* 服务器引擎         *)
    CLIENT_ENGINE_START                                 (* 客户端引擎         *)
  );

  (* 服务器侦听方式   *)
  TENGINE_LISTNER_TYPE = (
    SERVER_SINGLE_LISTNE,                               (*  单端口侦听        *)
    SERVER_DOUBLE_LISTNE                                (*  双端口侦听        *)
  );

  (* 协议类型         *)
  TSERVER_PROTOOOL_TYPE = (
    PROTOOOL_TCP,                                       (* TCP协议            *)
    PROTOOOL_UDP                                        (* UDP协议            *)
  );

  (* IO投递类型       *)
  IO_POST_EVENT_TYPE = (
    IO_INITIALIZE,                                      (* 初始化状态         *)
    IO_WSA_ACCEPTEX,                                    (* AcceptEx事件投递   *)
    IO_WSA_RECV,                                        (* 接收事件投递       *)
    IO_WSA_SEND,                                        (* 发送事件           *)
    IO_WSA_STREAM,                                      (* 流事件           *)
    IO_WSA_SHUTDOWN,                                    (* ShutDown事件       *)
    IO_WSA_CLOSESOCKET                                  (* CloseSocket事件    *)
  );

  (* IOSend投递返回类型 *)
  IO_SEND_STATUS = (
    IO_SEND_IO_PENDING,				                          (* PENDING            *)
    IO_SEND_WOULDBLOCK,                                 (* WOULDBLOCK         *)
    IO_SEND_SUCCESS,                                    (* SUCCESS            *)
    IO_SEND_LOCK,                                       (* LOCK               *)
    IO_SEND_RESET,                                      (* RESET              *)
    IO_SEND_ERROR                                       (* ERROR              *)
  );

  (* IORecv投递返回类型 *)
  IO_RECV_STATUS = (
    IO_RECV_IO_PENDING,				                          (* PENDING            *)
    IO_RECV_WOULDBLOCK,                                 (* WOULDBLOCK         *)
    IO_RECV_SUCCESS,                                    (* SUCCESS            *)
    IO_RECV_CLOSED,                                     (* CLOSED             *)
    IO_RECV_RESET,                                      (* RESET              *)
    IO_RECV_ERROR                                       (* ERROR              *)
  );

  (* Overlapped结构扩展 [底层通讯] *)
  PTOverLappedEx = ^TOverLappedEx;
  TOverLappedEx = packed record
    FOverlapped:        OVERLAPPED;                     (* 重叠               *)
    FIOType:            IO_POST_EVENT_TYPE;             (* IOType             *)
    FGUID:              TGUID;                          (* GUID               *)
    FRequest:           Pointer;                        (* per-I/O指针        *)
    FCallBackObj:       Pointer;                        (* 调用对象           *)
  end;

  //拼包状态
  TDATAPACK_STATE = (
    DP_LENGTH_ENOUGH,                           //有效长度包
    DP_LENGTH_NOENOUGH,                         //无效长度包(需要继续拼包)
    DP_LOGIC_VALID,                             //逻辑有效包(下一步拼包)
    DP_LOGIC_INVALID,                           //逻辑无效包(需要丢充包)
    DP_SPLIT_SUCCESS,                           //拼包成功
    DP_SPLIT_FAIL                               //拼包失败
  );

  //事件返回类型
  FN_HANDLE_STATUS = (
    FN_HANDLE_SUCCESS,                          //返回成功
    FN_HANDLE_FAIL,                             //返回失败
    FN_HANDLE_NOTEXIST,                         //方法不存在(数据包有误)
    FN_HANDLE_CONTINUE,                         //继续执行(数据包)
    FN_HANDLE_SPLIT_DATAPACK                    //函数调用时(数据包长度不够)
  );
  (*  *)

const
  MsgIO_POST_EVENT_TYPE: array [IO_INITIALIZE..IO_WSA_CLOSESOCKET] of String =
  (
    'IO_INITIALIZE',                                    (* 初始化状态         *)
    'IO_WSA_ACCEPTEX',                                  (* AcceptEx事件投递   *)
    'IO_WSA_RECV',                                      (* 接收事件投递       *)
    'IO_WSA_SEND',                                      (* 发送事件           *)
    'IO_WSA_STREAM',
    'IO_WSA_SHUTDOWN',                                  (* ShutDown事件       *)
    'IO_WSA_CLOSESOCKET'                                (* CloseSocket事件    *)
  );

//***********            【消息定义】           *************/
  //服务器消息级别
  SM_LEVEL_BASIC          =   100;                      //消息基数
  SM_LEVEL_REALTIME       =   100;                      //实时
  SM_LEVEL_HIGH           =   101;                      //高
  SM_LEVEL_IMPORTANCE     =   102;                      //重要
  SM_LEVEL_NORMAL         =   103;                      //正常
  SM_LEVEL_LOW            =   104;                      //低
  SM_LEVEL_IGNORE         =   105;                      //可忽略

//***********            【消息流向】           *************/
  //消息流向(Msg Direction)
  //(S->C, C->S, S->S, C->C, S->B, C->B)
  MD_SERVER_TO_CLIENT     =   220;                      //S->C
  MD_CLIENT_TO_SERVER     =   221;                      //C->S
  MD_SERVER_TO_SERVER     =   222;                      //S->S
  MD_CLIENT_TO_CLIENT     =   223;                      //C->C
  MD_SERVER_TO_BROADCAST  =   224;                      //S->B
  MD_CLIENT_TO_BROADCAST  =   225;                      //C->B

//***********            【业务类别标识】           *************/
  //业务类别(Business Kind)
  //(通用类别: 登录包, 时间包, 心跳包, 服务器列表包)
  //(聊天类别: 注册包, 登录包, 注销包, 用户列表包....)
  BK_NORMAL_SIGN          =   400;                      //通用类别
  BK_CHAT_SIGN            =   401;                      //聊天类别
  BK_FTP_SIGN             =   402;                      //FTP类别
  BK_DB_SIGN              =   403;                      //DB类别
  BK_MAIL_SIGN            =   404;                      //mail类别
  BK_MESSAGE_SIGN         =   405;                      //消息类别

//***********            【业务包标识】          ***********/
  //服务器端消息
  SM_REMOTE_BASIC         =   200;                      //服务器消息基类
  SM_REMOTE_LOGIN         =   200;                      //服务器登录
  SM_REMOTE_CALL    		  =   201;                      //服务器远程调用方法
  SM_REMOTE_HEARTBEAT     =   202;                      //服务器心跳消息
  SM_REMOTE_DOWNLOAD      =   203;                      //服务器下载消息
  SM_REMOTE_UPLOAD    	  =   204;                      //服务器上传消息
  SM_REMOTE_TIME          =   205;                      //服务器时间
  SM_REMOTE_SEVERLIST     =   206;                      //服务器列表

  //客户端消息
  CM_REMOTE_BASIC         =   300;
  CM_REMOTE_LOGIN         =   300;                      //客户端登录
  CM_REMOTE_CALL    		  =   301;                      //客户端远程调用方法
  CM_REMOTE_HEARTBEAT    	=   302;                      //客户端心跳消息
  CM_REMOTE_DOWNLOAD      =   303;                      //客户端下载消息
  CM_REMOTE_UPLOAD    	  =   304;                      //客户端上传消息
  CM_REMOTE_TIME      	  =   305;                      //客户端时间

implementation

end.
